package com.ryl.yomall.weball.controller;

import com.ryl.yomall.list.client.ListFeignClient;
import com.ryl.yomall.model.list.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 22:41
 * @Author ryl
 */
@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;
    @GetMapping("/list.html")
    public String list(SearchParam searchParam,Model model){

        //1. 回显关键词
        model.addAttribute ( "searchParam" ,searchParam);
        //2.选中项回显
        //选中品牌了
        String trademarkParam=buildTrademarkParam(searchParam);
        model.addAttribute ( "trademarkParam" ,trademarkParam);
        //选中平台属性
        List<Map> propsParamList = buildpropsParamList(searchParam);
        model.addAttribute("propsParamList", propsParamList);

        SearchResponseVo searchResponseVo = listFeignClient.list ( searchParam );
        //3.品牌集合(ID, 名称,logo)
        List<SearchResponseTmVo> trademarkList = searchResponseVo.getTrademarkList ( );
        model.addAttribute ( "trademarkList",trademarkList );
        //4.平台属性
        List<SearchResponseAttrVo> attrsList = searchResponseVo.getAttrsList ( );
        model.addAttribute ( "attrsList",attrsList );
        //5: 排序
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);
        //6.商品集合
        List<Goods> goodsList = searchResponseVo.getGoodsList ( );
        model.addAttribute ( "goodsList",goodsList );

        //7.分页
        model.addAttribute ( "pageNo",searchResponseVo.getPageNo () );
        model.addAttribute ( "totalPages",searchResponseVo.getTotalPages () );
        //8:urlParam   //https://list.yomall.com/list?keyword=手机&brand=苹果&props=3:8GB:内存
        String urlParam = buildUrlParam (searchParam );
        model.addAttribute ( "urlParam",urlParam );
        return "list/index";
    }

    //排序
    private Map buildOrderMap(SearchParam searchParam) {
        Map orderMap = new HashMap();
        //类型  综合 1

        String order = searchParam.getOrder();
        //现用户看的
        if(!StringUtils.isEmpty(order)){
            String[] o = order.split(":");
            orderMap.put("type",o[0]);
            orderMap.put("sort",o[1]);
        }else{
            //默认值
            orderMap.put("type","1");
            orderMap.put("sort","desc");
        }

        return orderMap;
    }
    //构建平台属性 被选中
    private List<Map> buildpropsParamList(SearchParam searchParam) {
        List<Map> listMap = new ArrayList<> ();
        // 0
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                Map map = new HashMap ();
                //prop  平台ID:平台值:平台属性名
                String[] p = prop.split(":");
                map.put("attrName",p[2]);
                map.put("attrValue",p[1]);
                map.put("attrId",p[0]);
                listMap.add(map);
            }
        }
        return listMap;
    }


    //选中品牌
    private String buildTrademarkParam(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            String[] t = trademark.split(":");
            return "品牌:" + t[1];
        }
        return null;
    }
    // 基础url
    private String buildUrlParam(SearchParam searchParam) {
        //字符串拼接
        StringBuilder sb = new StringBuilder();
        //url
        sb.append("/list.html?");
        //param  关键词 过滤条件
        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)) {
            sb.append("keyword=").append(keyword);
        }
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            sb.append("&trademark=").append(trademark);
        }
        //平台属性
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        return sb.toString();
    }

}
